1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use super::*;

/// A simple vector of substitutions
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct SubstVec<T = ()> {
    /// A vector of substitutions
    substs: Vec<TermOrShift>,
    /// The current number of substitutions
    no_subst: u32,
    /// The current lowest variable substituted
    base: u32,
    /// The typing context for this substitution vector
    ctx: T,
}

impl<T: TyCtxMut> SubstVec<T> {
    /// Create a new substitution vector with the given context
    #[inline]
    pub fn new(ctx: T) -> SubstVec<T> {
        SubstVec {
            substs: Vec::new(),
            no_subst: 0,
            base: 0,
            ctx,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum TermOrShift {
    Term(TermId),
    Shift(u32),
}

impl<T: TyCtxMut> SubstCtx for SubstVec<T> {
    type Ctx = T::MaxDeref;

    fn subst_var(&mut self, ix: u32, annot: Option<&TermId>) -> Result<Option<TermId>, Error> {
        if let Some(subst_ix) = ix.checked_sub(self.base) {
            let subst = if (subst_ix as usize) < self.substs.len() {
                self.substs[self.substs.len() - 1 - subst_ix as usize].clone()
            } else {
                TermOrShift::Shift(0)
            };
            match subst {
                TermOrShift::Term(term) => {
                    //TODO: transport
                    Ok(Some(term))
                }
                TermOrShift::Shift(shift) => {
                    let ty = annot.subst_rec(self)?;
                    let new_ix = subst_ix + shift - self.no_subst;
                    if ty.is_none() && new_ix == subst_ix {
                        return Ok(None);
                    }
                    let ty = ty
                        .unwrap_or_else(|| annot.consed(self.ctx.cons_ctx()))
                        .map(|ty| ty.into_shallow_cons(self.ctx.cons_ctx()));
                    let term = Var::new_unchecked(new_ix, ty).into_id_with(self.ctx.cons_ctx());
                    Ok(Some(term))
                }
            }
        } else {
            Ok(None)
        }
    }

    #[inline]
    fn push_param(&mut self, param_ty: Option<&TermId>) -> Result<Option<TermId>, Error> {
        let subst = if let Some(param_ty) = param_ty {
            param_ty.subst_rec(self)?
        } else {
            None
        };
        self.ctx.push_param(subst.as_ref().or(param_ty))?;
        self.base += 1;
        Ok(subst)
    }

    #[inline]
    fn intersects(&self, filter: VarFilter, _code: Code, _form: Form) -> bool {
        filter.fvb() >= self.base
    }

    #[inline]
    fn ctx(&mut self) -> &mut Self::Ctx {
        self.ctx.ctx()
    }

    #[inline]
    fn pop_param(&mut self) -> Result<(), Error> {
        self.ctx.pop_param()?;
        match self.substs.last() {
            Some(TermOrShift::Shift(_)) => {
                //TODO: check shift is base...
                self.substs.pop();
            }
            _ if self.base == 0 => return Err(Error::ParameterUnderflow),
            _ => self.base -= 1,
        }
        Ok(())
    }

    #[inline]
    fn is_var_null(&self) -> bool {
        self.substs.is_empty()
    }
}

impl<T: TyCtxMut> EvalCtx for SubstVec<T> {
    fn push_subst(&mut self, subst: TermId) -> Result<(), Error> {
        self.substs.reserve(self.base as usize + 1);
        while self.base > 0 {
            self.substs.push(TermOrShift::Shift(self.no_subst));
            self.base -= 1;
        }
        self.substs.push(TermOrShift::Term(subst));
        self.no_subst += 1;
        Ok(())
    }

    fn pop_subst(&mut self) -> Result<(), Error> {
        if self.base != 0 {
            return Err(Error::ParameterUnderflow);
        }
        match self.substs.last() {
            Some(TermOrShift::Term(_)) => {
                self.substs.pop();
                self.no_subst -= 1;
            }
            _ => {
                return Err(Error::ParameterUnderflow);
            }
        }
        while let Some(TermOrShift::Shift(s)) = self.substs.last() {
            if *s == self.no_subst {
                self.substs.pop();
                self.base += 1;
            } else {
                break;
            }
        }
        Ok(())
    }
}